package cn.manyufun.myf.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;

/**
 * Created by xiaow_000 on 2017-01-03.
 */

public class ObservableWebView extends WebView implements View.OnLongClickListener {
    private OnScrollChangedCallback mOnScrollChangedCallback;
    private LongClickCallBack mOnLongClickCallBack;
    public static final int SCROLL_DISTANCE = 100;

    public ObservableWebView(final Context context) {
        super(context);
        initWebview();
    }

    public ObservableWebView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        initWebview();
    }

    public ObservableWebView(final Context context, final AttributeSet attrs,
                             final int defStyle) {
        super(context, attrs, defStyle);
        initWebview();
    }

    private void initWebview() {
        setOnLongClickListener(this);
    }

//
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//
//        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//        if (heightMode == MeasureSpec.UNSPECIFIED) {
//            return;
//        }
//
//        final View child = this;
//        int height = getMeasuredHeight();
//        if (child.getMeasuredHeight() < height) {
//            final FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) child.getLayoutParams();
//
//            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
//                    getPaddingLeft() + getPaddingRight(), lp.width);
//            height -= getPaddingTop();
//            height -= getPaddingBottom();
//            int childHeightMeasureSpec =
//                    MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
//
//            child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
//            }
//
//    }

    @Override
    protected void onScrollChanged(final int l, final int t, final int oldl,
                                   final int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);

        if (mOnScrollChangedCallback != null) {
            mOnScrollChangedCallback.onScroll(l, t, l - oldl, t - oldt);
        }
    }

    public OnScrollChangedCallback getOnScrollChangedCallback() {
        return mOnScrollChangedCallback;
    }

    public void setOnScrollChangedCallback(
            final OnScrollChangedCallback onScrollChangedCallback) {
        mOnScrollChangedCallback = onScrollChangedCallback;
    }

    /**
     * Impliment in the activity/fragment/view that you want to listen to the webview
     */
    public interface OnScrollChangedCallback {
        public void onScroll(int x, int y, int dx, int dy);
    }

    public interface LongClickCallBack {
        void onLongClickCallBack(String imgUrl);
    }

    public void setOnLongClickCallback(
            LongClickCallBack onLongClickCallBack) {
        mOnLongClickCallBack = onLongClickCallBack;
    }

    @Override
    public boolean onLongClick(View v) {
        // 长按事件监听（注意：需要实现LongClickCallBack接口并传入对象）
        if (mOnLongClickCallBack == null)
            return true;
        final HitTestResult htr = getHitTestResult();//获取所点击的内容
        if (htr.getType() == WebView.HitTestResult.IMAGE_TYPE) {//判断被点击的类型为图片
            mOnLongClickCallBack.onLongClickCallBack(htr.getExtra());
        }
        return false;
    }


}